using System;
using System.Linq.Expressions;
using Randomize.Core.Constructors;

namespace Randomize.Core.Descriptors
{
    public class PropertyDescriptor<TComplex, TProperty, TTypeDescriptor> : PropertyDescriptorBase<TComplex> 
        where TTypeDescriptor : class, IGenerationDescriptor<TProperty, TTypeDescriptor>
    {
        public Expression<Func<TComplex, TProperty>> PropertyExpression { get; set; }

        public Action<TComplex, TProperty> Setter { get; set; }

        public IGenerationDescriptor<TProperty, TTypeDescriptor> TypeDescriptor { get; set; }

        public override IPropertyConstructor<TComplex> CreateConstructor(Random random)
        {
            var generator = TypeDescriptor.CreateGenerator(random);
            return new PropertyConstructor<TComplex, TProperty>(this.PropertyExpression, generator)
                   {
                       Setter = this.Setter
                   };
        }
    }
}